package Controlador.ComponentesCompuestos;

import Controlador.Controladores.FuenteTabla;
import java.awt.Color;
import java.awt.Component;
import java.util.Observable;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Observer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableRowSorter;

public final class TablaFuente extends JTable implements Observer{
    //Propiedades  del Objeto
    private Color color=new Color(0xEEEEF9);
    private FuenteTabla data =null;
    private ResultSet rs=null;
    private String[] TiposDatos={"Texto","Boleano","Numerico"};
    private List<String> Tipos=new ArrayList();
    //Otros Objetos de la Clase
    private DefaultTableCellRenderer forma = new DefaultTableCellRenderer() ;
    private  Component c =null;
    //Constructores
    public TablaFuente (){
        
    }

    //metodos
    @Override
     public void update(Observable o, Object o1) {
        rs=data.getRs();
        Modelo modelo =new Modelo();
        this.setModel(modelo);
        this.setRowSorter(new TableRowSorter(modelo));
    }
    
        
@Override
    public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
        c= super.prepareRenderer(renderer, row, col);
        if (row % 2 == 0 && !isCellSelected(row, col)) {
            c.setBackground(getColor());
        }
        else {
            if (isCellSelected(row, col)) {
                if (this.isEnabled()) 
                    c.setBackground(getSelectionBackground());
                else   
                    c.setBackground(new Color(0xC0C0C0));
                
                } else 
                    c.setBackground(getBackground());
                }
       return c;
    }

     //Geters y Seters
        /**
         * @return the Tipos
         */
        public List<String> getTipos() {
            return Tipos;
        }

        /**
         * @param Tipos the Tipos to set
         */
    

     public FuenteTabla getData() {
        return data;
    }


    public void setData(FuenteTabla data) {
        this.data = data;
        if(data!=null){
            this.getData().addObserver(this);
        }
    }

    public Color getColor() {
        return color;
    }


    private void setColor(Color color) {
        this.color = color;
    }

    /**
     * @param Tipos the Tipos to set
     */
    private void setTipos(List<String> Tipos) {
        
        this.Tipos = Tipos;
    }


    public String[] getTiposDatos() {
     
        
        return TiposDatos;
    }


    public void setTiposDatos(String[] TiposDatos) {
        this.TiposDatos = TiposDatos;
    }
    
    //clases Inner
    
    private class Modelo extends AbstractTableModel{
        private ResultSetMetaData rsMetaData;
         int nrow=0;
        public Modelo(){
          try {
              
                while(rs.next()) nrow++;
                rs.beforeFirst();
                System.out.println(nrow);
            } catch (SQLException ex) {
                nrow=0;
            }
        
        }
        
        
       @Override
        public int getRowCount() {
            return(nrow);
        }
       

        @Override
        public int getColumnCount() {
            int ncol = 0;
            try {
                rsMetaData= rs.getMetaData();
                ncol= rsMetaData.getColumnCount();
            } catch (SQLException ex) {
                ncol=0;
            }
             return ncol;
        }

        @Override
        public Object getValueAt(int i1, int i) {
            int cont=0;
                    int tam=TiposDatos.length;
        for (int i1t = 0; i1t < tam; i1t++) {
           Tipos.add(TiposDatos[i]);
        }
           try {
            rs.beforeFirst();
            while(rs.next()){
                if(cont==i1)
                    if(getTipos().get(i).equals("Boleano")){
                        return rs.getBoolean(i+1);
                    }else{
                        return rs.getString(i+1);
                }
                cont ++;
            }
                
             } catch (SQLException ex) {
                     return new String();
              }
            return new String();
        }

        @Override
        public Class<?> getColumnClass(int i1) {
            int tam=TiposDatos.length;
            for (int i = 0; i < tam; i++) {
           Tipos.add(TiposDatos[i]);
            }
           if(getTipos().get(i1).equals("Boleano")){
                        return  Boolean.class;
                    }else {
                         return  String.class;
                    }
        }

        @Override
        public String getColumnName(int i) {
            try {
                return rsMetaData.getColumnLabel(i+1);
            } catch (SQLException ex) {
                Logger.getLogger(TablaFuente.class.getName()).log(Level.SEVERE, null, ex);
            }
            return("");
        }

     
        
    }
     

    
}
